import requestUtil from "@/utils/requestUtil";
import { IObject } from "@/api/common/common";

const INVENTORY_BASE_URL = "/business/inventory";

const InventoryAPI = {
  /** 获取仓库分页数据 */
  getPage(queryParams?: InventoryPageQuery) {
    return requestUtil.get<PageResult<InventoryPageVO[]>>(
      `${INVENTORY_BASE_URL}/loadPage`,
      queryParams
    );
  },

  /**
   * 获取仓库表单数据
   *
   * @param id 仓库ID
   * @returns 仓库表单数据
   */
  getFormData(id: string) {
    return requestUtil.get<InventoryForm>(`${INVENTORY_BASE_URL}/load/${id}`);
  },

  /** 仓库操作 */
  create(data: InventoryForm) {
    return requestUtil.post<InventoryForm>(`${INVENTORY_BASE_URL}/operation`, data);
  },

  /**
   * 更新仓库
   *
   * @param id 仓库ID
   * @param data 仓库表单数据
   */
  update(id: string, data: InventoryForm) {
    return requestUtil.post<InventoryForm>(`${INVENTORY_BASE_URL}/update`, data);
  },

  /**
   * 批量删除仓库，多个以英文逗号(,)分割
   *
   * @param ids 仓库ID字符串，多个以英文逗号(,)分割
   */
  deleteByIds(ids: string) {
    return requestUtil.get<number>(`${INVENTORY_BASE_URL}/batchDelete?ids=${ids}`);
  },

  deleteById(id: string) {
    return requestUtil.get<number>(`${INVENTORY_BASE_URL}/delete/${id}`);
  },
  configSaleMeal(configSaleMealForm: ConfigSaleMealForm) {
    return requestUtil.get<boolean>(`${INVENTORY_BASE_URL}/configSaleMeal`, configSaleMealForm);
  },

  loadByMaterial(materialId: string, warehouseId?: string, type?: string) {
    return requestUtil.get<InventoryPageVO>(
      `${INVENTORY_BASE_URL}/loadByMaterial?materialId=${materialId}&warehouseId=${warehouseId}&type=${type}`
    );
  }
};

export default InventoryAPI;

/** 分页查询参数 */
export interface InventoryPageQuery extends PageQuery {
  /** 搜索关键字 */
  warehouseId?: string;
  sku?: string;
  type?: string;
}

/** 分页对象 */
export interface InventoryPageVO {
  /** ID */
  id?: string;
  materialId?: string;
  sku?: string;
  warehouseName?: string;
  quantity?: number;
  /** 锁定数量 */
  lockQuantity?: number;
  name?: string;
  unit?: string;
  spec?: string;
  price?: string;
  costPrice?: string;
  num?: number;
  type?: string;
}

/** 库存记录表单对象 */
export interface InventoryForm {
  /** ID */
  id?: string;
  /** 操作 */
  operation?: string;
  /** 标题 */
  title?: string;
  remark?: string;
  items?: InventoryItemForm[];
  warehouseId?: string;
}

/** 库存记录表单对象 */
export interface InventoryItemForm {
  /** ID */
  id?: string;
  /** 材料id */
  materialId?: string;
  /** 标题 */
  sku?: string;
  /** 数量 */
  quantity?: number;

  /** 类型 */
  type?: string;
}

/** 库存记录表单对象 */
export interface ConfigSaleMealForm {
  /** 材料id */
  saleMealId?: string;
  /** 标题 */
  warehouseId?: string;
  /** 数量 */
  quantity?: number;
  /** 操作0增加，1减少 */
  operation?:number;
}

export enum InventoryType {
  MAIN = "MAIN",
  SUB = "SUB",
  INSTRUMENT = "INSTRUMENT",
  OTHER = "OTHER",
  SALE_MEAL = "SALE_MEAL",
}

export const Inventory_LABEL: IObject = {
  [InventoryType.MAIN]: "主材",
  [InventoryType.SUB]: "辅材",
  [InventoryType.INSTRUMENT]: "器具",
  [InventoryType.OTHER]: "其他",
  [InventoryType.SALE_MEAL]: "套餐",
};
